"""
-------------------------------
- File_Name: student.py
- Date: 2025/2/12
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description:
-------------------------------
"""
from fastapi import APIRouter, Path, Body, Query, Depends
from starlette.requests import Request

from app.models.student import Student
from app.schemas.student import *
from app.schemas import BaseResponse

from app.utils.responseUtil import ResponseUtil
from app.utils.ormUtil import OrmCRUD
from app.core.security import get_current_user


router = APIRouter(dependencies=[Depends(get_current_user)])
prefix = "/student"
tags = ["学生信息"]
description = "学生信息相关接口"


@router.post("/student",summary="新增学生信息",response_model=BaseResponse[StudentOut])
async def post_student(request: Request,student_in: StudentIn):
    try:
        stu_crud = OrmCRUD(Student)
        stu_obj = await stu_crud.add(**student_in.model_dump())
        return ResponseUtil.success_response(request,stu_obj, msg=f"创建学生【{stu_obj}】成功")
    except Exception as e:
        return ResponseUtil.handle_exception(request,e, msg=f"创建学生异常")



@router.delete("/student/{id}/",summary="删除学生信息", response_model=BaseResponse[StudentOut])
async def delete_student(request: Request,id: str = Path(description="学生id", alias='id')) -> dict:
    try:
        stu_crud = OrmCRUD(Student)
        stu_obj = await stu_crud.delete(id=id)
        return ResponseUtil.success_response(request,stu_obj, msg=f"删除学生【{stu_obj.name}】成功")
    except Exception as e:
        return ResponseUtil.handle_exception(request,e, msg=f"删除学生异常")



@router.put("/student/{id}/",summary="更新学生信息", response_model=BaseResponse[StudentOut])
async def update_student(request: Request,id: str = Path(description="学生id"),student_update: StudentUpdate = Body(...)):
    try:
        stu_crud = OrmCRUD(Student)
        print('student_update:', student_update)
        stu_obj = await stu_crud.update(data=student_update, id=id)
        return ResponseUtil.success_response(request,stu_obj,msg=f"更新学生【{stu_obj.name}】成功")
    except Exception as e:
        return ResponseUtil.handle_exception(request,e, msg=f"更新学生【id={id}】异常")



@router.get("/student",summary="查询所有学生信息",response_model=BaseResponse[StudentOut])
async def get_student(
        request: Request,
        stu_name: str = Query("", description="学生名称"),
        stu_note: str = Query("", description="描述")
):
    try:
        stu_crud = OrmCRUD(Student)
        stu_obj = await stu_crud.filter(name__startswith=stu_name, note__contains=stu_note)
        return ResponseUtil.success_response(request, data=stu_obj, msg="查询学生成功")
    except Exception as e:
        # 统一异常处理
        return ResponseUtil.handle_exception(request, e, msg="查询所有学生异常")



@router.get("/student/{id}",summary="查询某一学生信息",response_model=BaseResponse[StudentOut])
async def get_student(request: Request, id: int = Path(description="学生id")):
    try:
        stu_crud = OrmCRUD(Student)
        stu_obj = await stu_crud.get_or_none(id=id)
        return ResponseUtil.success_response(request, data=stu_obj, msg=f"查询学生【{stu_obj.name}】成功")
    except Exception as e:
        # 统一异常处理
        return ResponseUtil.handle_exception(request, e, msg="查询学生异常")



@router.post("/student/cd", summary="测试用例")
async def post_cd_student(request: Request):

    stu_obj = await Student.get_or_none(name="铁牛")
    print('stu_obj', stu_obj)
    return ')))k'